package main

import (
	"bufio"
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"

	"gitee.com/xuender/findzip"
)

func main() {
	// csvFile, _ := os.Open("/home/ender/文档/执行结果1.txt")
	// csvFile, err := os.Open("/home/ender/文档/0j1ax-bmuhr.csv")
	csvFile, err := os.Open("/home/ender/文档/2021-07-29-14-57-22_EXPORT_CSV_3132928_398_0.csv")
	if err != nil {
		log.Fatal("err1", err)
	}
	// csvFile, _ := os.Open("/home/ender/文档/2021-05-28-09-43-00_EXPORT_CSV_2690935_995/a.csv")
	reader := csv.NewReader(bufio.NewReader(csvFile))
	data := []map[string]string{}
	var title []string
	i := 5
	for {
		line, err := reader.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal("err: ", err)
		}
		if title == nil {
			title = line
			continue
		}
		m := map[string]string{}
		for i, t := range title {
			if line[i] == "" {
				continue
			}
			m[t] = findzip.ToText(t, line[i])
		}
		data = append(data, m)
		if len(data) > 100000 {
			j, _ := json.Marshal(data)
			f, _ := os.Create(fmt.Sprintf("1405211_%d.json", i))
			defer f.Close()
			f.Write(j)
			data = []map[string]string{}
			i++
		}
	}
	j, _ := json.Marshal(data)
	f, _ := os.Create("1405211.json")
	defer f.Close()
	f.Write(j)
	log.Println("完毕")
}
